package com.xinbochuang.machine.admin.service.impl.macRoom;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.macRoom.*;
import com.xinbochuang.machine.admin.domain.system.SysDept;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.macRoom.UlocationMapper;
import com.xinbochuang.machine.admin.service.macRoom.IMacRoomService;
import com.xinbochuang.machine.admin.service.macRoom.IUlocationService;
import com.xinbochuang.machine.admin.service.system.ISysDeptService;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杜文博
 * @date 2020-09-08
 */
@Service
public class UlocationServiceImpl extends ServiceImpl<UlocationMapper, Ulocation> implements IUlocationService {
    @Autowired
    private UlocationMapper ulocationMapper;

    @Resource
    private TokenService tokenService;

    @Resource
    private ISysDeptService deptService;

    @Resource
    private IMacRoomService macRoomService;

    /**
     * 这写的啥？？？
     * 头疼
     * 不想看
     * 这个方法很神奇，假分页，查所有数据然后返回对应页数的数据 (○´･д･)ﾉ
     * -- hxp
     */
    @Override
    public TableDataInfo getUlocationMacRoom(UlocationStatic ulocationStatic) {
        Integer he = null;
        if (ulocationStatic.getHeight() != null) {
            try {
                he = Integer.valueOf(ulocationStatic.getHeight());
            } catch (Exception e) {
            }
        }
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
       List<UlocationStatic> staticList = ulocationMapper.getUlocationStatic(ulocationStatic.getMacId(), ulocationStatic.getMacName(), ulocationStatic.getCabinetName(), he, user.getDeptId() + "");
        Map<String, UlocationMacRoomStatic> map = new HashMap<>();
        List<String> allIds = new ArrayList<>();
        for (UlocationStatic ulocationStatic1 : staticList) {
            // 总u位数
            int total = (ulocationStatic1.getUlocationQuantity() == null) ? 0 : ulocationStatic1.getUlocationQuantity();
            // 已用
            int used = (ulocationStatic1.getHeight() == null) ? 0 : ulocationStatic1.getHeight();
            // 空闲
            int other = total - used;

            if (!map.containsKey(ulocationStatic1.getMacId())) {
                UlocationMacRoomStatic ulocationMacRoomStatic = new UlocationMacRoomStatic();
                ulocationMacRoomStatic.setMacId(ulocationStatic1.getMacId());
                MacRoom macRoom = macRoomService.getById(ulocationStatic1.getMacId());
                SysDept dept = deptService.getById(macRoom.getDeptId());
                ulocationMacRoomStatic.setDeptId(dept.getDeptId());
                ulocationMacRoomStatic.setDeptName(dept.getDeptName());
                ulocationMacRoomStatic.setMacName(ulocationStatic1.getMacName());
                ulocationMacRoomStatic.setUlocationQuantity(total);
                ulocationMacRoomStatic.setHeight(used);
                ulocationMacRoomStatic.setOtherHeight(other);
                map.put(ulocationStatic1.getMacId(), ulocationMacRoomStatic);
            } else {
                UlocationMacRoomStatic ulocationMacRoomStatic = map.get(ulocationStatic1.getMacId());
                ulocationMacRoomStatic.setUlocationQuantity(ulocationMacRoomStatic.getUlocationQuantity() + total);
                ulocationMacRoomStatic.setHeight(ulocationMacRoomStatic.getHeight() + used);
                ulocationMacRoomStatic.setOtherHeight(ulocationMacRoomStatic.getUlocationQuantity() - ulocationMacRoomStatic.getHeight());
            }
            if (he != null) {
                int count = 0;
                if (StringUtils.isBlank(ulocationStatic1.getEquHeight())) {
                    if (!allIds.contains(ulocationStatic1.getMacId())) {
                        allIds.add(ulocationStatic1.getMacId());
                    }
                    continue;
                }
                String[] equHeights = ulocationStatic1.getEquHeight().split(",");
                for (int i = 1; i <= ulocationStatic1.getUlocationQuantity(); i++) {
                    for (String equHeight : equHeights) {
                        String[] splits = equHeight.split("-");
                        int start = Integer.valueOf(splits[0]);
                        int end = Integer.valueOf(splits[1]);
                        if (i >= start && i <= end) {
                            count = -1;
                        }

                    }
                    count++;
                    if (count >= he) {
                        if (!allIds.contains(ulocationStatic1.getMacId())) {
                            allIds.add(ulocationStatic1.getMacId());
                        }
                        break;
                    }
                }
            } else {
                if (!allIds.contains(ulocationStatic1.getMacId())) {
                    allIds.add(ulocationStatic1.getMacId());
                }
            }
        }
        List<UlocationMacRoomStatic> ulocationMacRoomStatics = new ArrayList<>();
        for (String id : allIds) {
            ulocationMacRoomStatics.add(map.get(id));
        }

        // 不知道循环判断了半天啥，只查出了和u位表有关联的机房记录，这里直接查其他机房~~
        QueryWrapper<MacRoom> queryWrapper = new QueryWrapper<>();
        if (!allIds.isEmpty()) {
            queryWrapper.lambda().notIn(MacRoom::getUuid, allIds);
        }
        // 数据权限
        List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        if (!deptIds.isEmpty()) {
            queryWrapper.lambda().in(MacRoom::getDeptId, deptIds);
        }
        if (StrUtil.isNotBlank(ulocationStatic.getMacName())) {
            queryWrapper.lambda().like(MacRoom::getMacName, ulocationStatic.getMacName());
        }
        List<MacRoom> macRoomList = macRoomService.list(queryWrapper);
        for (MacRoom macRoom : macRoomList) {
            UlocationMacRoomStatic u = new UlocationMacRoomStatic();
            u.setMacId(macRoom.getUuid());
            u.setMacName(macRoom.getMacName());
            if (macRoom.getDeptId() != null) {
                u.setDeptId(macRoom.getDeptId());
                SysDept dept = deptService.getById(macRoom.getDeptId());
                u.setDeptName(dept.getDeptName());
            }
            u.setUlocationQuantity(47);
            u.setHeight(0);
            u.setOtherHeight(47);
            ulocationMacRoomStatics.add(u);
        }

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(ulocationMacRoomStatics.size());
        if (ulocationMacRoomStatics.size() == 0) {
            return tableDataInfo;
        } else if (ulocationMacRoomStatics.size() >= ulocationStatic.getPageSize() * ulocationStatic.getPageNum() - 1) {
            tableDataInfo.setRows(ulocationMacRoomStatics.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), ulocationStatic.getPageNum() * ulocationStatic.getPageSize() - 1));
        } else {
            tableDataInfo.setRows(ulocationMacRoomStatics.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), ulocationMacRoomStatics.size()));
        }
        return tableDataInfo;
    }

    @Override
    public TableDataInfo getUlocationMacRoomNew(UlocationStatic ulocationStatic) {
        Integer he = null;
        if (ulocationStatic.getHeight() != null) {
            try {
                he = Integer.valueOf(ulocationStatic.getHeight());
            } catch (Exception e) {
            }
        }
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String deptId =user.getDeptId() + "";
        if(user.getUsername().equals("admin")){
            deptId="";
        }
        List<UlocationMacRoomStatic> ulocationMacRoomStatics = ulocationMapper.getUlocationStaticNew(deptId, ulocationStatic.getMacName(), he );

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(ulocationMacRoomStatics.size());
        if (ulocationMacRoomStatics.size() == 0) {
            return tableDataInfo;
        } else if (ulocationMacRoomStatics.size() >= ulocationStatic.getPageSize() * ulocationStatic.getPageNum() - 1) {
            tableDataInfo.setRows(ulocationMacRoomStatics.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), ulocationStatic.getPageNum() * ulocationStatic.getPageSize() - 1));
        } else {
            tableDataInfo.setRows(ulocationMacRoomStatics.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), ulocationMacRoomStatics.size()));
        }
        return tableDataInfo;
    }


    @Override
    public TableDataInfo getUlocationStatic(UlocationStatic ulocationStatic) {
        Integer he = null;
        if (ulocationStatic.getHeight() != null) {
            try {
                he = Integer.valueOf(ulocationStatic.getHeight());
            } catch (Exception e) {
            }
        }
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<UlocationStatic> staticList = ulocationMapper.getUlocationStatic(ulocationStatic.getMacId(), ulocationStatic.getMacName(), ulocationStatic.getCabinetName(), he, user.getDeptId() + "");
        List<UlocationStatic> result = new ArrayList<>();
        for (UlocationStatic ulocationStatic1 : staticList) {
            if (he != null) {
                int count = 0;
                if (StringUtils.isBlank(ulocationStatic1.getEquHeight())) {
                    result.add(ulocationStatic1);
                    continue;
                }
                String[] equHeights = ulocationStatic1.getEquHeight().split(",");
                for (int i = 1; i <= ulocationStatic1.getUlocationQuantity(); i++) {
                    for (String equHeight : equHeights) {
                        String[] splits = equHeight.split("-");
                        int start = Integer.valueOf(splits[0]);
                        int end = Integer.valueOf(splits[1]);
                        if (i >= start && i <= end) {
                            count = -1;
                        }

                    }
                    count++;
                    if (count >= he) {
                        result.add(ulocationStatic1);
                        break;
                    }
                }
            } else {
                result.add(ulocationStatic1);
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(result.size());
        if (result.size() == 0) {
            return tableDataInfo;
        } else if (result.size() >= ulocationStatic.getPageSize() * ulocationStatic.getPageNum() - 1) {
            tableDataInfo.setRows(result.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), ulocationStatic.getPageNum() * ulocationStatic.getPageSize() - 1));
        } else {
            tableDataInfo.setRows(result.subList((ulocationStatic.getPageNum() - 1) * ulocationStatic.getPageSize(), result.size()));
        }
        return tableDataInfo;
    }

    @Override
    public Map<String, Object> getUlocationMacRoomByChart(UlocationStatic ulocationStatic) {
        Integer he = null;
        if (ulocationStatic.getHeight() != null) {
            try {
                he = Integer.valueOf(ulocationStatic.getHeight());
            } catch (Exception e) {
            }
        }
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<UlocationStatic> staticList = ulocationMapper.getUlocationStatic(ulocationStatic.getMacId(), ulocationStatic.getMacName(), ulocationStatic.getCabinetName(), he, user.getDeptId() + "");
        Map<String, UlocationMacRoomStatic> map = new HashMap<>();
        List<String> allIds = new ArrayList<>();
        for (UlocationStatic ulocationStatic1 : staticList) {
            if (!map.containsKey(ulocationStatic1.getMacId())) {
                UlocationMacRoomStatic ulocationMacRoomStatic = new UlocationMacRoomStatic();
                ulocationMacRoomStatic.setMacId(ulocationStatic1.getMacId());
                ulocationMacRoomStatic.setMacName(ulocationStatic1.getMacName());
                ulocationMacRoomStatic.setUlocationQuantity(0);
                ulocationMacRoomStatic.setHeight(0);
                ulocationMacRoomStatic.setOtherHeight(0);
                map.put(ulocationStatic1.getMacId(), ulocationMacRoomStatic);
            }
            UlocationMacRoomStatic ulocationMacRoomStatic = map.get(ulocationStatic1.getMacId());
            if (StringUtils.isNotBlank(ulocationStatic1.getEquHeight())) {
                ulocationMacRoomStatic.setUlocationQuantity(ulocationMacRoomStatic.getUlocationQuantity() + ulocationStatic1.getUlocationQuantity());
                ulocationMacRoomStatic.setHeight(ulocationMacRoomStatic.getHeight() + ulocationStatic1.getHeight());
                ulocationMacRoomStatic.setOtherHeight(ulocationMacRoomStatic.getUlocationQuantity() - ulocationMacRoomStatic.getHeight());
            }
            if (he != null) {
                int count = 0;
                if (StringUtils.isBlank(ulocationStatic1.getEquHeight())) {
                    if (!allIds.contains(ulocationStatic1.getMacId())) {
                        allIds.add(ulocationStatic1.getMacId());
                    }
                    continue;
                }
                String[] equHeights = ulocationStatic1.getEquHeight().split(",");
                for (int i = 1; i <= ulocationStatic1.getUlocationQuantity(); i++) {
                    for (String equHeight : equHeights) {
                        String[] splits = equHeight.split("-");
                        int start = Integer.valueOf(splits[0]);
                        int end = Integer.valueOf(splits[1]);
                        if (i >= start && i <= end) {
                            count = -1;
                        }

                    }
                    count++;
                    if (count >= he) {
                        if (!allIds.contains(ulocationStatic1.getMacId())) {
                            allIds.add(ulocationStatic1.getMacId());
                        }
                        break;
                    }
                }
            } else {
                if (!allIds.contains(ulocationStatic1.getMacId())) {
                    allIds.add(ulocationStatic1.getMacId());
                }
            }
        }
        List<UlocationMacRoomStatic> ulocationMacRoomStatics = new ArrayList<>();
        for (String id : allIds) {
            ulocationMacRoomStatics.add(map.get(id));
        }
        List<String> names = new ArrayList<>();
        List<Integer> num = new ArrayList<>();
        for (UlocationMacRoomStatic ulocationMacRoomStatic : ulocationMacRoomStatics) {
            names.add(ulocationMacRoomStatic.getMacName());
            num.add(ulocationMacRoomStatic.getOtherHeight());
        }
        Map<String, Object> result = new HashMap<>();
        result.put("name", names);
        result.put("value", num);
        return result;
    }

    /**
     * 机房u位统计
     *
     * @return 结果
     */
    @Override
    public List<UlocationCount> uLocaiontCount() {
        //List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        Integer  deptId =999 ;
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(!user.getUsername().equals("admin")){
            deptId =user.getDeptId();
        }
        return ulocationMapper.selectUlocationCount(deptId);
    }
}
